package com.lxk.service.impl;

import com.lxk.dao.GoodsStatMapper;
import com.lxk.pojo.GoodsStat;
import com.lxk.pojo.GoodsStatExample;
import com.lxk.pojo.GoodsStatExample.Criteria;
import com.lxk.service.GoodsStatService;
import com.lxk.util.exception.GlobalException;
import com.lxk.vo.result.CodeMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class GoodsStatServiceImpl implements GoodsStatService {
	
	@Autowired
	private GoodsStatMapper gsm;
	
	private GoodsStat selByGoodsId(Integer goodsId) {
		GoodsStatExample gse = new GoodsStatExample();
		Criteria criteria = gse.createCriteria();
		criteria.andGoodsIdEqualTo(goodsId);
		List<GoodsStat> list = gsm.selectByExample(gse);
		if(list.size()>0) {
			return list.get(0);
		}else {
			throw new GlobalException(CodeMsg.NO_GOODS_STAT);
		}
	}
	
	
	private boolean isSetAvailable(GoodsStat stat, Integer code) {
		if(code==0) {
			return true;
		}else {
			Integer isAuction = stat.getIsAuction();
			Integer isOnsale = stat.getIsOnsale();
			Integer isSeckill = stat.getIsSeckill();
			if(isAuction==1||isOnsale==1||isSeckill==1) {
				return false;
			}
			return true;
		}
	}
	
	
	public Integer setGoodsUpper(Integer goodsId, Integer code) {
		GoodsStat stat = selByGoodsId(goodsId);
		stat.setIsUpper(code);
		return gsm.updateByPrimaryKeySelective(stat);
	}

	public Integer setGoodsAuction(Integer goodsId, Integer code) {
		GoodsStat stat = selByGoodsId(goodsId);
		if(!isSetAvailable(stat, code)) {
			throw new GlobalException(CodeMsg.GOODS_STAT_REPEAT);
		}
		stat.setIsAuction(code);
		if(code==0) {
			stat.setIsUpper(1);
		}else {
			stat.setIsUpper(0);
		}
		return gsm.updateByPrimaryKeySelective(stat);
	}

	public Integer setGoodsSeckill(Integer goodsId, Integer code) {
		GoodsStat stat = selByGoodsId(goodsId);
		if(!isSetAvailable(stat, code)) {
			throw new GlobalException(CodeMsg.GOODS_STAT_REPEAT);
		}
		stat.setIsSeckill(code);
		if(code==0) {
			stat.setIsUpper(1);
		}else {
			stat.setIsUpper(0);
		}
		return gsm.updateByPrimaryKeySelective(stat);
	}

	public Integer setGoodsOnsale(Integer goodsId, Integer code) {
		GoodsStat stat = selByGoodsId(goodsId);
		if(!isSetAvailable(stat, code)) {
			throw new GlobalException(CodeMsg.GOODS_STAT_REPEAT);
		}
		stat.setIsOnsale(code);
		return gsm.updateByPrimaryKeySelective(stat);
	}

	public Integer setGoodsVip(Integer goodsId, Integer code) {
		GoodsStat stat = selByGoodsId(goodsId);
		stat.setIsVip(code);
		return gsm.updateByPrimaryKeySelective(stat);
	}

	public GoodsStat selStatByGoodsId(Integer goodsId) {
		GoodsStatExample example = new GoodsStatExample();
		GoodsStatExample.Criteria criteria = example.createCriteria();
		criteria.andGoodsIdEqualTo(goodsId);
		List<GoodsStat> list = gsm.selectByExample(example);
		if (list.size()<=0) {
			throw new GlobalException(CodeMsg.NO_GOODS_STAT);
		}
		GoodsStat goodsStat = list.get(0);
		return goodsStat;
	}

	public void updateGoodsStat(GoodsStat goodsStat) {
		gsm.updateByPrimaryKeySelective(goodsStat);
	}

	public boolean isSeckillExist() {
		GoodsStatExample example = new GoodsStatExample();
		GoodsStatExample.Criteria criteria = example.createCriteria();
		criteria.andIsSeckillEqualTo(1);
		long count = gsm.countByExample(example);
		if (count == 0) {
			return false;
		}else{
			return true;
		}
	}

	public boolean isAuctionExist() {
		GoodsStatExample example = new GoodsStatExample();
		GoodsStatExample.Criteria criteria = example.createCriteria();
		criteria.andIsAuctionEqualTo(1);
		long count = gsm.countByExample(example);
		if (count == 0) {
			return false;
		}else{
			return true;
		}
	}

}
